<template>
  <el-select
    v-model="currentValue"
    placeholder="请选择题库"
    clearable
    filterable
    :multiple="multiple"
    :disabled="disabled"
    @change="handleChange"
  >
    <el-option
      v-for="bank in questionBankList"
      :key="bank.bankId"
      :label="bank.bankName"
      :value="bank.bankId"
    />
  </el-select>
</template>

<script>
import { listAllQuestionBank } from "@/api/exam/questionBank";

export default {
  name: "QuestionBankSelect",
  props: {
    value: {
      type: [String, Number, Array],
      default: null
    },
    disabled: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      questionBankList: [],
      currentValue: this.value
    };
  },
  watch: {
    value(newVal) {
      this.currentValue = newVal;
    }
  },
  created() {
    this.getQuestionBankList();
  },
  methods: {
    /** 获取题库列表 */
    getQuestionBankList() {
      listAllQuestionBank().then(response => {
        this.questionBankList = response.rows || response.data || [];
      }).catch(error => {
        console.error('获取题库列表失败:', error);
      });
    },
    /** 选择变化处理 */
    handleChange(value) {
      this.currentValue = value;
      this.$emit('input', value);
      this.$emit('change', value);
    }
  }
};
</script> 